package me.j360.springboot.simple.bean.search.rule;

import me.j360.springboot.simple.bean.BaseEnum;
import org.springframework.util.Assert;

/**
 * 说明：
 * Bean类 - 查询条件（Sql）
 *
 * ============================================================================
 * 版权所有 2015 qiushihua。
 *
 * @author qiushihua
 * @version 0.1 2015-07-22
 * ============================================================================
 */
public class RuleRelation extends Rule {

    private final Rule[] rules;

    private RuleRelation(BaseEnum.RuleType ruleType, Rule[] rules) {
        super(ruleType);

        this.rules = rules;
    }

    public Rule[] getRules() {
        return rules;
    }

    private static Builder newBuilder() {
        return new Builder();
    }

    public static RuleRelation and(Rule... rules) {
        return newBuilder().setRuleType(BaseEnum.RuleType.and).setRules(rules).build();
    }

    public static RuleRelation or(Rule... rules) {
        return newBuilder().setRuleType(BaseEnum.RuleType.or).setRules(rules).build();
    }

    public static class Builder extends Rule.Builder<Rule, Builder> {

        private Rule[] rules;

        @Override
        protected Builder getThis() {
            return this;
        }

        public Builder setRuleType(BaseEnum.RuleType ruleType) {
            this.ruleType = ruleType;
            return this;
        }

        public Builder setRules(Rule... rules) {
            this.rules = rules;
            return this;
        }

        @Override
        public RuleRelation build() {
            Assert.notNull(ruleType, "ruleType 不能为空");
            Assert.notEmpty(rules, "ruleType 不能为空");
            return new RuleRelation(ruleType, rules);
        }

    }

}
